package com.cpi.cornpp.purchasesrealized.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futurepurchase.exception.FuturePurchaseException;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.purchasesrealized.form.FuturePurchasesRealizedForm;
/**
 * Compras Realizadas Action
 * @author Edgar Joao
 *
 */
public class FuturePurchasesRealizedAction extends MappingDispatchAction{
	/** Identifier default workflow*/
	private static final String DEFAULT = "default";//$NON-NLS-1$
	private static final String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String PURCHASE_INSTRUCTION_FUTURE_ID_PARAMETER = "purchaseInstructionFutureId";//$NON-NLS-1$	
	private static final String FUTURE_PURCHASES_REALIZED_LIST = "FUTURE_PURCHASES_REALIZED_LIST";//$NON-NLS-1$
	
	/**
	 * Ir al Listado de Compras Realizadas
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward go(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {		
		HttpSession httpSession = null;
		FuturePurchaseService service = null;
		FuturePurchasesRealizedForm form = null;
		List futurePurchasesRealizedList = null;		
		String purchaseInstructionFutureId = null;
		try{
			service = new FuturePurchaseService();
			form = (FuturePurchasesRealizedForm) argForm;
			futurePurchasesRealizedList = new ArrayList(0);
			httpSession = argRequest.getSession();		
			purchaseInstructionFutureId = (String) argRequest.getParameter(PURCHASE_INSTRUCTION_FUTURE_ID_PARAMETER);
			if(purchaseInstructionFutureId == null){
				purchaseInstructionFutureId = (String)httpSession.getAttribute(PURCHASE_INSTRUCTION_FUTURE_ID_PARAMETER);				
			}else{
				httpSession.setAttribute(PURCHASE_INSTRUCTION_FUTURE_ID_PARAMETER, purchaseInstructionFutureId);
			}
			futurePurchasesRealizedList = service.getFuturePurchasesRealizedList(Integer.parseInt(purchaseInstructionFutureId), form.getOrderField(),(1 == form.getOrderType()) ? false : true);
			argRequest.setAttribute(FUTURE_PURCHASES_REALIZED_LIST, futurePurchasesRealizedList);			
			httpSession.setAttribute("previousOrderField", Integer.toString(form.getOrderField()));
			httpSession.setAttribute("previousOrderType", Integer.toString(form.getOrderType()));			
			
		}catch (FuturePurchaseException futurePurchaseException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, futurePurchaseException);
			throw futurePurchaseException;
		}catch (Exception e){
			FuturePurchaseException futurePurchaseException = null;
			e.printStackTrace(System.out);
			futurePurchaseException= new FuturePurchaseException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, futurePurchaseException);
			throw futurePurchaseException;
		}			
		argForm.reset(argMapping, argRequest);
		return argMapping.findForward(DEFAULT);
	}
}
